import { defineStore } from "pinia";
import Cache from "@/utils/cache";
export const useBusinessIdStore = defineStore(
  "business_id",
  () => {
    // business_id
    const business_id = ref("");
    function setBusinId(str: string) {
      console.log("设置business_id", str);

      business_id.value = str;
    }

    // 清除用户信息
    function clearBusinId() {
      business_id.value = "";
    }

    return {
      business_id,
      setBusinId,
      clearBusinId,
    };
  },
  {
    persist: {
      key: "business_id",
      storage: {
        setItem(key, value) {
          // 直接存储 business_id 字符串值而不是整个对象
          Cache.set(key, value);
        },
        getItem(key) {
          return Cache.get(key);
        },
      },
      // 只序列化和反序列化 business_id 字段
      serializer: {
        serialize: (value) => {
          // 只存储 business_id 的值
          return value.business_id;
        },
        deserialize: (value) => {
          // 从存储中读取时，构造成正确的对象格式
          const business_idValue = JSON.parse(value);
          return { business_id: business_idValue };
        },
      },
    },
  }
);
